﻿using Microsoft.Phone.Controls;
using System;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace ReservationChecker
{
    public partial class DetailsPage : PhoneApplicationPage
    {
        // Constructor
        public DetailsPage()
        {
            InitializeComponent();

            // Set the data context of the LongListSelector control to the Details data
            DataContext = App.DetailsViewModel;
        }

        private static DateTime QueryDate;

        public static DateTime queryDate          //Accessible property to set and find the               
        {
            get                       
            {
                return QueryDate;
            }

            set                        
            {
                QueryDate = value;		   
            }
        }  

        // Load data for the ViewModel Items
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string selectedDate = "";
            if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedDate))        // gets value from string passed with URI
            {
                queryDate = Convert.ToDateTime(selectedDate);                // sets the queryDate as the DateTime equivalent of the passed selectedDate
                App.DetailsViewModel.IsDataLoaded = false;                   // sets IsDataLoaded to false to allow it to reload queries using the back button
            }

            if (!App.DetailsViewModel.IsDataLoaded)
            {
                App.DetailsViewModel.LoadData();
            }
            string format = "dddd, dd-MMMM";                                           //format date for Details page
            detailsTextBlock.Text = "Details for " + queryDate.ToString(format);    //setting date on details page to selectedDate that's been passed in.
        }
    }
}